﻿package game 
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import game.exchangeddata.HashTable;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class SoundController
	{
		public var soundChannel:SoundChannel;
		public var activeSound:Boolean;
		//var sound:Sound;
		public function SoundController() {
				soundChannel = new SoundChannel();
				this.activeSound  = true;
		}
		
		/**
		 * @author	Bamboo
		 * @param	mp3
		 */
		public function playSound(mp3:String, volume:Number):void
        {
			//trace("this.activeSound:" + this.activeSound);
			if (this.activeSound)
			{
				//trace("mp3:" + mp3 + "volume:" + volume);
				if (soundChannel) {				
					soundChannel.stop();
					soundChannel.removeEventListener(Event.SOUND_COMPLETE, hdlStopSound);
				}
				var sound:Sound = new Sound();
				sound.load(new URLRequest(mp3));			
				soundChannel = sound.play();
				setVolumnSound(volume);
				soundChannel.addEventListener(Event.SOUND_COMPLETE, hdlSoundComplete);	
			}
        }
		/**
		 * @author	Bamboo
		 * @param	event
		 */
		private function onSoundLoaded(event:Event):void
		{
			var localSound:Sound = event.target as Sound;
			localSound.play();
		}
		
		public function playMusic(mp3:String):void {
			if (soundChannel) {				
				soundChannel.stop();
				soundChannel.removeEventListener(Event.SOUND_COMPLETE, hdlStopSound);
			}
				
			var sound:Sound = new Sound();
			sound.load(new URLRequest(mp3));			
			soundChannel = sound.play();
			setVolumnSound(GlobalVariables.volumeSoundBackground);
			soundChannel.addEventListener(Event.SOUND_COMPLETE, hdlSoundComplete);		
		}
		
		private function hdlStopSound(e:Event):void {
			
		}
		
		private function hdlSoundComplete(e:Event):void {
			
		}
		
		public function stopSound():void {
			//trace("vo stop");
			soundChannel.stop();
			this.activeSound = false;
		}
		public function unLockSound():void {
			//trace("vo activeSound");
			this.activeSound = true;
		}
		
		public function setVolumnSound(volume:Number):void {	
			var soundTransform = new SoundTransform();
			soundTransform.volume = volume/100;
			soundChannel.soundTransform = soundTransform;	
			//trace("setVolumnSound" + volume)
		}
	}
}